package client.ui.theme.defaultTheme;

import java.util.ArrayList;
import java.util.Iterator;

import server.BuddyList;
import server.Group;
import server.Person;
import server.Status;

/**
 * Class DefaultInviteWindow
 *
 * @author Tomas
 * @date March 14 2009
 */
public class DefaultInviteWindow extends javax.swing.JFrame {

    private DefaultChatRoomTab parent = null;
	
    public DefaultInviteWindow(DefaultChatRoomTab p, BuddyList bl) {
    	parent = p;
        initComponents();
        displayBuddyList(bl);
    }

    private void displayBuddyList(BuddyList bl) {

    	ArrayList<String> buddyNames = new ArrayList<String>();
    	Iterator<Group> itgroup = bl.getGroups();
    	
    	while(itgroup.hasNext()){
    		Group g = itgroup.next();
    		Iterator<Person> it = g.getMembers();
    		
    		while(it.hasNext()){
    			Person p = it.next();
    			if(p.getStatus() == Status.ONLINE){
    				buddyNames.add(p.getUsername());
    			}
    		}
    	}
		
    	if(buddyNames.size() != 0){
    		String[] names = new String[buddyNames.size()];
    		buddyNames.toArray(names);
    		final String[] content = names;
    		inviteList.setModel(new javax.swing.AbstractListModel() {
                String[] strings = content;
                public int getSize() { return strings.length; }
                public Object getElementAt(int i) { return strings[i]; }
            });
    	}
	}

	private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        inviteList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Parlanchin - Invite buddy");
        setAlwaysOnTop(true);
        setResizable(false);

        inviteList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                inviteListMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(inviteList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }

    private void inviteListMouseClicked(java.awt.event.MouseEvent evt) {

    	String name = (String) inviteList.getSelectedValue();
    	
    	if(name != null && !name.equals("")){
    		parent.invite(name);
    		this.dispose();
    	}
    }

    private javax.swing.JList inviteList;
    private javax.swing.JScrollPane jScrollPane1;

}
